package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxShopInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.YxShopInfoRepository;
import co.yixiang.modules.shop.service.YxShopInfoService;
import co.yixiang.modules.shop.service.dto.YxShopInfoDto;
import co.yixiang.modules.shop.service.dto.YxShopInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxShopInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "yxShopInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxShopInfoServiceImpl implements YxShopInfoService {

    @Resource
    private YxShopInfoRepository yxShopInfoRepository;
    @Resource
    private YxShopInfoMapper yxShopInfoMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxShopInfoQueryCriteria criteria, Pageable pageable){
        Page<YxShopInfo> page = yxShopInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxShopInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxShopInfoDto> queryAll(YxShopInfoQueryCriteria criteria){
        return yxShopInfoMapper.toDto(yxShopInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxShopInfoDto findById(Integer id) {
        YxShopInfo yxShopInfo = yxShopInfoRepository.findById(id).orElseGet(YxShopInfo::new);
        ValidationUtil.isNull(yxShopInfo.getId(),"YxShopInfo","id",id);
        return yxShopInfoMapper.toDto(yxShopInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxShopInfoDto create(YxShopInfo resources) {
        return yxShopInfoMapper.toDto(yxShopInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxShopInfo resources) {
        YxShopInfo yxShopInfo = yxShopInfoRepository.findById(resources.getId()).orElseGet(YxShopInfo::new);
        ValidationUtil.isNull( yxShopInfo.getId(),"YxShopInfo","id",resources.getId());
        yxShopInfo.copy(resources);
        yxShopInfoRepository.save(yxShopInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxShopInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YxShopInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxShopInfoDto yxShopInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", yxShopInfo.getName());
            map.put("联系电话", yxShopInfo.getContactPhone());
            map.put("联系人", yxShopInfo.getContactPerson());
            map.put("归属部门ID", yxShopInfo.getDeptId());
            map.put("状态（0：关闭，1：开启）", yxShopInfo.getStatus());
            map.put("添加时间", yxShopInfo.getAddTime());
            map.put("改变时间", yxShopInfo.getChangeTime());
            map.put("是否删除", yxShopInfo.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}